package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.phone.R;
import com.qiantu.phone.ui.adapter.RoomFloorDialogAdapter;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class BottomAreaSelectDialog_Energy extends BottomPopupView implements View.OnClickListener {
    private ImageView close;
    private AppCompatTextView allArea;
    private int allRoomCount;
    private boolean allAreaIsSelect = true;
    private RecyclerView floorRoomList;
    private RoomFloorDialogAdapter mAdapter;
    private Map<String, RoomBean> mSelectArea = new LinkedHashMap<>();
    private Map<String, RoomBean> mSaveArea = new LinkedHashMap<>();
    private List<FloorBean> mFloorBeans;

    public BottomAreaSelectDialog_Energy(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.bottom_select_area;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        allArea = findViewById(R.id.all_area);
        floorRoomList = findViewById(R.id.floor_room_list);
        floorRoomList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new RoomFloorDialogAdapter(getContext()) {
            @Override
            public void onSelectChanged(Map<String, RoomBean> selectBean) {
                allArea.setSelected(selectBean.isEmpty() || selectBean.size() == allRoomCount);
                if (selectBean.size() == allRoomCount){
                    mAdapter.clearSelected();
                }
            }
        };
        mAdapter.setHasAllItem(true);
        floorRoomList.setAdapter(mAdapter);
        mAdapter.setData(mFloorBeans);
        findViewById(R.id.save).setOnClickListener(this::onClick);
        findViewById(R.id.close).setOnClickListener(this::onClick);
        findViewById(R.id.reset).setOnClickListener(this::onClick);
        findViewById(R.id.all_area).setOnClickListener(this::onClick);
    }

    public BottomAreaSelectDialog_Energy setFloorBeans(List<FloorBean> data) {
        allRoomCount = LLRoomDBManager.getInstance(getContext()).getAll().size();
        mFloorBeans = data;
        if (mAdapter != null) {
            mAdapter.setData(mFloorBeans);
        }
        return this;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void beforeShow() {
        super.beforeShow();
        initSelectState();
    }

    private void initSelectState() {
        mSelectArea.clear();
        mSelectArea.putAll(mSaveArea);
        mAdapter.setSelectMap(mSelectArea);
        allArea.setSelected(allAreaIsSelect);
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return getResources().getDimensionPixelOffset(R.dimen.dp_600);
    }

    public void onSave(boolean isQueryHouse, Map<String, RoomBean> result) {}

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.save){
            mSaveArea.clear();
            mSaveArea.putAll(mSelectArea);
            allAreaIsSelect = allArea.isSelected();
            if (allAreaIsSelect){
                onSave(true, null);
            }else {
                onSave(false, mSaveArea);
            }
            dismiss();
        }else if (id == R.id.close){
            dismiss();
        }else if (id == R.id.all_area){
            allArea.setSelected(true);
            mAdapter.clearSelected();
        }else if (id == R.id.reset){
            allArea.setSelected(true);
            mAdapter.clearSelected();
        }
    }

    public Map<String, String> getSelectAreaTitle(){
        return mAdapter.getSelectAreaTitle();
    }
}
